package com.pioneer.beans;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import org.jdom.Element;
import org.jdom.Document;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class Manifest {

	private String jobNum;
	private String manifestType;
	private String vessel;	
	private Date etd;
	private Date eta;
	private String containerNotes;
	private float freight;
	private float trucking;
	private float misc;
	private List containerList=new ArrayList();
	private DateFormat formatter;

    public Manifest(String jobNum,	String manifestType, String vessel,	Date etd,
	Date eta, String containerNotes, float freight, float trucking, float misc) {
		this.jobNum=jobNum;
		this.manifestType = manifestType;
		this.vessel=vessel;
		this.eta=eta;
		this.etd=etd;
		this.containerNotes=containerNotes;
		this.freight=freight;
		this.trucking = trucking;
		this.misc = misc;
		formatter=new SimpleDateFormat("MM/dd/yyyy");
    }

	public Manifest(String jobNum, String manifestType,	String vessel,	String etdString,
	String etaString, String containerNotes, String freightString, String truckingString, String miscString)
	throws ParseException,NumberFormatException {
		this.jobNum=jobNum;
		this.manifestType = manifestType;
		this.vessel=vessel;
		formatter=new SimpleDateFormat("MM/dd/yyyy");
		this.eta=formatter.parse(etaString);
		this.etd=formatter.parse(etdString);
		if (freightString==null) throw new NumberFormatException("Cannot pass null to float");
		this.freight=new Float(freightString).floatValue();
		this.trucking = new Float(truckingString).floatValue();
		this.misc = new Float(miscString).floatValue();
		this.containerNotes=containerNotes;
    }

	public Document getDocRep() {
		Element rootElement=new Element("Manifest");
		rootElement.addContent(new Element("JobNum").addContent(jobNum));
		rootElement.addContent(new Element("Vessel").addContent(vessel));
		rootElement.addContent(new Element("Etd").addContent(getEtdString()));
		rootElement.addContent(new Element("Eta").addContent(getEtaString()));
		rootElement.addContent(new Element("ContainerNotes").addContent(containerNotes));
		rootElement.addContent(new Element("Freight").addContent(getFreightString()));
		rootElement.addContent(new Element("Trucking").addContent(getTruckingString()));
		rootElement.addContent(new Element("Misc").addContent(getMiscString()));
		Element containerListElement=new Element("ContainerList");
		for (int containerCount=0;containerCount<containerList.size();containerCount++) {
			Element containerElement=new Element("Container");
			Container container=(Container)containerList.get(containerCount);
			containerElement.addContent(new Element("Id").addContent(container.getId()));
			Element contentListElement=new Element("ContentList");
			List contentList=container.getContents();
			for (int contentCount=0;contentCount<contentList.size();contentCount++) {
				Element containerContentElement=new Element("Content");
				ContainerContent containerContent=(ContainerContent)contentList.get(contentCount);
				containerContentElement.addContent(new Element("Description").addContent(containerContent.getDescription()));
				containerContentElement.addContent(new Element("Weight").addContent(Float.toString(containerContent.getWeight())));
				containerContentElement.addContent(new Element("Fob").addContent(containerContent.getFob()));
				containerContentElement.addContent(new Element("FobCNumber").addContent(Float.toString(containerContent.getFobCNumber())));
				// new business logic from Frank 2005-02-07
				float cost=containerContent.getCost();
				float fobAmount=cost*containerContent.getWeight();
				// end logic
				containerContentElement.addContent(new Element("FobAmount").addContent(Float.toString(fobAmount)));
				containerContentElement.addContent(new Element("Cost").addContent(Float.toString(cost)));
				containerContentElement.addContent(new Element("Remarks").addContent(containerContent.getRemarks()));
				contentListElement.addContent(containerContentElement);
			}
			containerElement.addContent(contentListElement);
			containerListElement.addContent(containerElement);
		}
		rootElement.addContent(containerListElement);
		return new Document(rootElement);
	}

	public String getJobNum() {
		return this.jobNum;
	}

	public void setJobNum(String jobNum) {
		this.jobNum=jobNum;
	}

	public String getManifestType() {
		return this.manifestType;
	}

	public void setManifestType(String manifestType) {
		this.manifestType=manifestType;
	}
	
	public String getVessel() {
		return this.vessel;
	}

	public void setVessel(String vessel) {
		this.vessel=vessel;
	}

	public Date getEtd() {
		return this.etd;
	}

	public String getEtdString() {
		return formatter.format(etd);
	}

	public void setEtdString(String etdString)
	throws ParseException {
		this.etd=formatter.parse(etdString);
	}

	public Date getEta() {
		return this.eta;
	}

	public String getEtaString() {
		return formatter.format(eta);
	}

	public void setEtaString(String etaString)
	throws ParseException {
		this.eta=formatter.parse(etaString);
	}

	public String getContainerNotes() {
		return this.containerNotes;
	}

	public void setContainerNotes(String notes) {
		this.containerNotes=notes;
	}

	public float getFreight() {
		return this.freight;
	}

	public String getFreightString() {
		return Float.toString(this.freight);
	}
	
	public float getTrucking() {
		return this.trucking;
	}
	
	public String getTruckingString() {
		return Float.toString(this.trucking);
	}

	public float getMisc() {
		return this.misc;
	}
	
	public String getMiscString() {
		return Float.toString(this.misc);
	}

	public void setFreight(float freight) {
		this.freight=freight;
	}

	public void setTrucking(float trucking) {
		this.trucking=trucking;
	}
	
	public void setMisc(float misc) {
		this.misc=misc;
	}
	
	public void addContainer(Container content)
	{
		containerList.add(content);
	}

	public boolean deleteContainer(String description)
	{
		return containerList.remove(getContainer(description));
	}

	public Container[] getContainers()
	{
		Object[] contents=containerList.toArray();
		Container[] contentArr=new Container[contents.length];
		for (int count=0;count<contents.length;count++)
		{
			contentArr[count]=(Container)contentArr[count];
		}
		return contentArr;
	}

	protected Container getContainer(String id)
	{
		Container foundContainer=null;
		Iterator contents=containerList.iterator();
		while (contents.hasNext())
		{
			Container content=(Container)contents.next();
			if (content.getId().equals(id))
			{
				foundContainer=content;
				break;
			}
		}
		return foundContainer;
	}

}